package com.sitanyun.merchant.guide.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.UntreatedAdapter;
import com.sitanyun.merchant.guide.base.BaseFragment;
import com.sitanyun.merchant.guide.bean.ForgetsmscoedeBean;
import com.sitanyun.merchant.guide.bean.UnitreateBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.frament.view.activity.UserMoneyActivity;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.view.activity.SceneActivity;
import com.sitanyun.merchant.guide.view.activity.VrWebActivity;
import com.sitanyun.merchant.guide.weiht.PhoneUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuikit.tuicontact.bean.ContactItemBean;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 * 未处理
 */
public class UntreatedFragment extends BaseFragment {


    @BindView(R.id.rv_untreated)
    RecyclerView rvUntreated;
    @BindView(R.id.sw_untreated)
    SwipeRefreshLayout swUntreated;
    @BindView(R.id.oneyy)
    LinearLayout oneyy;
    @BindView(R.id.ll_todaytwo)
    LinearLayout llTodaytwo;
    // 分页
    private int offset = 1;
    private UntreatedAdapter untreatedAdapter;
    private List<UnitreateBean.DataBean.RecordsBean> orderbeanlist;
    private UnitreateBean unitreateBean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_untreated;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        //创建布局管理
        orderbeanlist = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvUntreated.setLayoutManager(layoutManager);
        untreatedAdapter = new UntreatedAdapter(getActivity());
        rvUntreated.setAdapter(untreatedAdapter);
        untreatedAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        offset = 1;
        initokhttps();
    }

    //获取数据
    private void initokhttps() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("size", "10");
        hashMap.put("current", String.valueOf(offset));
        hashMap.put("readFlag", "0");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.page)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {
                swUntreated.setRefreshing(false);
            }

            @Override
            public void onResponse(String response, int id) {
                swUntreated.setRefreshing(false);
                unitreateBean = new Gson().fromJson(response, UnitreateBean.class);
                if (unitreateBean.getCode() == 0) {

                    if (offset == 1) {
                        if (unitreateBean.getData().getRecords().size()==0){
                            oneyy.setVisibility(View.GONE);
                            llTodaytwo.setVisibility(View.VISIBLE);
                            return;
                        }else {
                            llTodaytwo.setVisibility(View.GONE);
                            oneyy.setVisibility(View.VISIBLE);
                        }
                        orderbeanlist.clear();
                        untreatedAdapter.setNewData(unitreateBean.getData().getRecords());
                        orderbeanlist.addAll(unitreateBean.getData().getRecords());
                        untreatedAdapter.notifyDataSetChanged();
                    } else {
                        //orderbeanlist.addAll(orderBean.getData());
                        //底部没有数据时
                        if (unitreateBean.getData().getRecords().isEmpty()) {
                            untreatedAdapter.loadMoreEnd();
                        } else {
                            //滑动加载数据
                            List<UnitreateBean.DataBean.RecordsBean> data1 = unitreateBean.getData().getRecords();
                            orderbeanlist.addAll(data1);
                            untreatedAdapter.addData(data1);
                            untreatedAdapter.notifyDataSetChanged();
                            untreatedAdapter.loadMoreComplete();

                        }

                    }
                } else {

                }

            }
        });

    }

    @Override
    public void initView() {
        //刷新
        swUntreated.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                offset = 1;
                initokhttps();
            }
        });
        //加载更多
        untreatedAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                offset++;
                initokhttps();
            }
        });
//点击跳转    // 用户行为 浏览 留言 要求回电 同屏讲解 分享
        //    // enum('BROWSE','MESSAGE','CALL','MEETING','SHARE')
        untreatedAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    //更新消息为已处理
                    case R.id.ll_layout:

                        initread(orderbeanlist.get(position).getId());
                        if (orderbeanlist.get(position).getBizId()==0){
                            if (orderbeanlist.get(position).getBehavior().getBehavior().equals("BROWSE")) {
                                startActivity(new Intent(getActivity(), VrWebActivity.class)
                                        .putExtra("type", "2")
                                        .putExtra("names", orderbeanlist.get(position).getBehavior().getSceneName())
                                        .putExtra("storeid", SharedPreferenceUtil.getStringData("merchantid"))
                                        .putExtra("shops", SharedPreferenceUtil.getStringData("merchant"))
                                        .putExtra("scenid", orderbeanlist.get(position).getBehavior().getSceneId() + "")
                                        .putExtra("starfid", orderbeanlist.get(position).getBehavior().getStaffId() + "")
                                        .putExtra("starfname", orderbeanlist.get(position).getBehavior().getStaffName() + ""));

                                //liulan
                            } else if (orderbeanlist.get(position).getBehavior().getBehavior().equals("MESSAGE")) {
                                Bundle bundle = new Bundle();
                                bundle.putString(TUIConstants.TUIChat.CHAT_ID, "cid_" + orderbeanlist.get(position).getBehavior().getCId());
                                bundle.putString(TUIConstants.TUIChat.CHAT_NAME, orderbeanlist.get(position).getBehavior().getCName());
                                bundle.putInt(TUIConstants.TUIChat.CHAT_TYPE, ContactItemBean.TYPE_C2C);
                                TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, bundle);
                                //xiaoxi
                            } else if (orderbeanlist.get(position).getBehavior().getBehavior().equals("CALL")) {
                                PhoneUtils.callPhone(getActivity(), orderbeanlist.get(position).getBehavior().getCPhone());

                                //dianhua
                            } else if (orderbeanlist.get(position).getBehavior().getBehavior().equals("MEETING")) {
                                //tongpin
                                startActivity(new Intent(getActivity(), SceneActivity.class).putExtra("id",2));


                            } else if (orderbeanlist.get(position).getBehavior().getBehavior().equals("SHARE")) {
                                //分享

                            }
                        }else {
                            startActivity(new Intent(getActivity(), UserMoneyActivity.class));
                        }

                        break;
                }
            }
        });
    }

    //更新消息为已处理
    private void initread(String bicid) {
        Map<String, List> hashMap = new HashMap<>();
        List lists = new ArrayList<>();
        lists.add(bicid);
        hashMap.put("list", lists);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.setIsRead)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ForgetsmscoedeBean baseEntity = new Gson().fromJson(response, ForgetsmscoedeBean.class);
                if (baseEntity.getCode() == 0) {
                    Log.e("TAGS", "已处理");
                }
            }
        });

    }

}
